package com.yamaikeji.web.controller.demo.controller;

import com.microsoft.cognitiveservices.speech.*;
import com.microsoft.cognitiveservices.speech.audio.*;
import java.util.concurrent.ExecutionException;

public class CreateSynthesis {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        String speechKey = "CmdpjcxYyxwr1OqxFilmX1y9YHCX6ToKesFUJUe2yXHLVJUuZuQTJQQJ99BCAC1i4TkXJ3w3AAAYACOGSRfA";
        String speechRegion = "centralus";
        String outputPath = "D:/output.mp3"; // 保存到D盘根目录

        // 1. 配置语音合成器
        SpeechConfig speechConfig = SpeechConfig.fromSubscription(speechKey, speechRegion);
        speechConfig.setSpeechSynthesisVoiceName("en-US-AvaMultilingualNeural");

        // 2. 配置音频输出（保存到文件）
        AudioConfig audioConfig = AudioConfig.fromWavFileOutput(outputPath);

        // 3. 创建SSML文本（包含书签和视觉标记）
        String ssml = String.format("<speak version='1.0' xml:lang='en-US' xmlns='http://www.w3.org/2001/10/synthesis' xmlns:mstts='http://www.w3.org/2001/mstts'>"
                + "<voice name='en-US-AvaMultilingualNeural'>"
                + "<mstts:viseme type='redlips_front'/>"
                + "The rainbow has seven colors: <bookmark mark='colors_list_begin'/>Red, orange, yellow, green, blue, indigo, and violet<bookmark mark='colors_list_end'/>."
                + "</voice></speak>");

        // 4. 执行合成并保存
        try (SpeechSynthesizer synthesizer = new SpeechSynthesizer(speechConfig, audioConfig)) {
            SpeechSynthesisResult result = synthesizer.SpeakSsmlAsync(ssml).get();

            if (result.getReason() == ResultReason.SynthesizingAudioCompleted) {
                System.out.println("音频已保存至: " + outputPath);
            } else {
                System.out.println("合成失败: " + result.getReason());
            }
        }
    }
}